package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.constant.IpAddressConstant;
import com.ruoyi.system.domain.ERP.CustomerErpDTO;
import com.ruoyi.system.domain.ERP.DeptErpDTO;
import com.ruoyi.system.domain.ERP.UsersErpDTO;
import com.ruoyi.system.domain.InterfaceLog;
import com.ruoyi.system.domain.MES.OrderInfoUpDTO;
import com.ruoyi.system.domain.base.*;
import com.ruoyi.system.domain.client.BaseCustomerSupplier;
import com.ruoyi.system.enums.RabbitMqEnum;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.utils.rabbitMq.direct.DirectSender;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IBaseBomService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 物料清单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Service
@Component("erpTask")
public class BaseBomServiceImpl extends ServiceImpl<BaseBomMapper, BaseBom> implements IBaseBomService {

    @Autowired
    private BaseBomInfoMapper bomInfoMapper;
    @Autowired
    private InterfaceLogMapper interfaceLogMapper;
    @Autowired
    private DirectSender myProducer;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String BomInfoSync(List<BaseBomDTO> list) {
        if(IterUtil.isEmpty(list)){
            throw new CustomException("所传bom数据为空");
        }
        list.stream().forEach(i->{
            BaseBom baseBom = new BaseBom();
            BeanUtil.copyProperties(i,baseBom);
            baseMapper.insert(baseBom);

            List<BaseBomInfoDTO> listInfo = i.getList();
            if(IterUtil.isEmpty(listInfo)){
                throw new CustomException("所传bom详情为空");
            }

            listInfo.stream().forEach(j->{
                BaseBomInfo baseBomInfo = new BaseBomInfo();
                BeanUtil.copyProperties(j,baseBomInfo);
                bomInfoMapper.insert(baseBomInfo);
            });

        });
        //日志记录
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("WMS->MOM");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(list));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse("WMSBOM数据同步接收成功");
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateBy(SecurityUtils.getUsername());
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLogMapper.insert(interfaceLog);
        return "同步成功";
    }



    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getUsers() {
        System.out.println("==========用户信息抓取定时任务开始===========");
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String res= HttpUtil.get(IpAddressConstant.ERP_IP+"/basic/person/syncPersonList", paramMap);
        Map map = JSONUtil.toBean(res, Map.class);
        List data = (List)map.get("data");
        String codestr = (String)map.get("code");

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("WMS->MOM");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(paramMap));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(res);
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("获取用户信息");
        interfaceLogMapper.insert(interfaceLog);

        if(IterUtil.isNotEmpty(data)&&"10000".equals(codestr)){
            JSONArray array = JSONUtil.parseArray(JSONUtil.toJsonStr(map.get("data")));
            List<UsersErpDTO> userList = JSONUtil.toList(array, UsersErpDTO.class);

            //将用户数据同步到mes
            this.sedUserMes(userList);

            //进行用户新增
            userList.stream().forEach(i->{
                SysUser sysUser = new SysUser();
                sysUser.setUserName(i.getJobNum());
                sysUser.setNickName(i.getPersonName());
                sysUser.setDeptId(Long.valueOf(i.getDeptNum()));
                sysUser.setSex(i.getSex());
                if("true".equals(i.getDelFlag())){
                    sysUser.setDelFlag("2");//2代表删除
                }
                if(StrUtil.isNotEmpty(i.getUpdateDate())){
                    sysUser.setUpdateTime(DateUtil.parse(i.getUpdateDate().replace("/","-"),"yyyy-MM-dd"));
                }
                SysUser use = sysUserMapper.selectUserById(sysUser.getUserId());
                if(ObjectUtil.isNotNull(use)){
                    sysUserMapper.updateUser(sysUser);
                }else {
                    sysUserMapper.insertUser(sysUser);
                }
            });


            //同步成功之后进行回调
            HashMap<String, Object> paramMapRes = new HashMap<>();
            List<String> pks = userList.stream().map(i -> i.getPk()).collect(Collectors.toList());
            paramMapRes.put("codes", pks);
            paramMapRes.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
            String resp= HttpUtil.post(IpAddressConstant.ERP_IP+"/basic/person/syncStateUpdate", JSONUtil.toJsonStr(paramMapRes));
            Map mapResp = JSONUtil.toBean(resp, Map.class);
            String code = (String)mapResp.get("code");

            //添加日志
            InterfaceLog interfaceLog2 = new InterfaceLog();
            interfaceLog2.setSerialNumber(String.valueOf(new Date().getTime()));
            interfaceLog2.setStartTime(new Date());
            interfaceLog2.setInterfaceDirection("WMS->MOM");
            interfaceLog2.setPacketSize("");
            interfaceLog2.setInterfaceRequest(JSONUtil.toJsonStr(paramMapRes));
            interfaceLog2.setEndTime(new Date());
            interfaceLog2.setInterfaceResponse(resp);
            long be2 = DateUtil.between(interfaceLog2.getStartTime(), interfaceLog2.getEndTime(), DateUnit.MS);
            interfaceLog2.setTimeConsuming(String.valueOf(be2));
            interfaceLog2.setCreateTime(new Date());
            interfaceLog2.setInterfaceType("http");
            interfaceLog2.setInterfaceName("获取用户信息回调");
            interfaceLogMapper.insert(interfaceLog2);

        }
        System.out.println("==========用户信息抓取定时任务结束===========");
    }


    /**
     * 用户数据发送至MES
     * @param userList
     */
    private void sedUserMes(List<UsersErpDTO> userList) {
        OrderInfoUpDTO orderInfoUpDTO = new OrderInfoUpDTO();
        orderInfoUpDTO.setInterface("USER_INFO");
        orderInfoUpDTO.setBody(userList);
        myProducer.send(RabbitMqEnum.USER_INFO.getCode(),JSONUtil.toJsonStr(userList));

        //日志记录
        InterfaceLog interfaceLog2 = new InterfaceLog();
        interfaceLog2.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog2.setStartTime(new Date());
        interfaceLog2.setInterfaceDirection("MOM->MES");
        interfaceLog2.setPacketSize("");
        interfaceLog2.setInterfaceRequest(JSONUtil.toJsonStr(userList));
        interfaceLog2.setEndTime(new Date());
        interfaceLog2.setInterfaceResponse("发送成功");
        long be2 = DateUtil.between(interfaceLog2.getStartTime(), interfaceLog2.getEndTime(), DateUnit.MS);
        interfaceLog2.setTimeConsuming(String.valueOf(be2));
        interfaceLog2.setCreateTime(new Date());
        interfaceLog2.setInterfaceType("activeMq");
        interfaceLog2.setInterfaceName("用户信息同步至MES");
        interfaceLogMapper.insert(interfaceLog2);

    }


    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getDepts() {
        System.out.println("==========部门信息抓取定时任务开始===========");
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String res= HttpUtil.get(IpAddressConstant.ERP_IP+"/basic/dept/syncDeptList", paramMap);
        Map map = JSONUtil.toBean(res, Map.class);
        List data = (List)map.get("data");
        String codestr = (String)map.get("code");

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("MOM->ERP");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(paramMap));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(JSONUtil.toJsonStr(res));
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLogMapper.insert(interfaceLog);

        if(IterUtil.isNotEmpty(data)&&"10000".equals(codestr)){
            JSONArray array = JSONUtil.parseArray(JSONUtil.toJsonStr(map.get("data")));
            List<DeptErpDTO> deptList = JSONUtil.toList(array, DeptErpDTO.class);
            //进行部门新增
            deptList.stream().forEach(i->{
                SysDept sysDept = new SysDept();
                String deptCode = i.getDeptCode();
                if(deptCode.length()==2){
                    sysDept.setParentId(0L);
                }else if(StrUtil.isNotEmpty(deptCode)){
                    sysDept.setParentId(Long.valueOf(deptCode.substring(0,deptCode.length()-2)));
                }
                sysDept.setDeptId(Long.valueOf(deptCode));
                sysDept.setDeptName(i.getDeptName());
                sysDept.setPhone(i.getDeptTel());
                if("true".equals(i.getDelFlag())){
                    sysDept.setDelFlag("2");//2代表删除
                }
                sysDept.setOrderNum(i.getDeptGrade());
                SysDept de = sysDeptMapper.selectDeptById(sysDept.getDeptId());
                if(ObjectUtil.isNotNull(de)){
                    sysDeptMapper.updateDept(sysDept);
                }else {
                    sysDeptMapper.insertDept(sysDept);
                }
            });


            //同步成功之后进行回调
            HashMap<String, Object> paramMapRes = new HashMap<>();
            List<String> deptids = deptList.stream().map(i -> i.getDeptId()).collect(Collectors.toList());
            paramMapRes.put("codes", deptids);
            paramMapRes.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
            String resp= HttpUtil.post(IpAddressConstant.ERP_IP+"/basic/dept/syncStateUpdate", JSONUtil.toJsonStr(paramMapRes));
            Map mapResp = JSONUtil.toBean(resp, Map.class);
            String code = (String)mapResp.get("code");

            //添加日志
            InterfaceLog interfaceLog2 = new InterfaceLog();
            interfaceLog2.setSerialNumber(String.valueOf(new Date().getTime()));
            interfaceLog2.setStartTime(new Date());
            interfaceLog2.setInterfaceDirection("MOM->ERP");
            interfaceLog2.setPacketSize("");
            interfaceLog2.setInterfaceRequest(JSONUtil.toJsonStr(paramMapRes));
            interfaceLog2.setEndTime(new Date());
            interfaceLog2.setInterfaceResponse(JSONUtil.toJsonStr(resp));
            long be2 = DateUtil.between(interfaceLog2.getStartTime(), interfaceLog2.getEndTime(), DateUnit.MS);
            interfaceLog2.setTimeConsuming(String.valueOf(be2));
            interfaceLog2.setCreateTime(new Date());
            interfaceLog2.setInterfaceType("http");
            interfaceLogMapper.insert(interfaceLog2);
        }
        System.out.println("==========部门信息抓取定时任务结束===========");
    }

    @Autowired
    private BaseCustomerSupplierMapper baseCustomerSupplierMapper;

    @Override
    public String getCustomer() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String res= HttpUtil.get(IpAddressConstant.ERP_IP+"/basic/customer/syncCustomerList", paramMap);
        Map map = JSONUtil.toBean(res, Map.class);
        List data = (List)map.get("data");
        String codestr = (String)map.get("code");

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("MOM->ERP");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(paramMap));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(JSONUtil.toJsonStr(res));
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("客户数据同步");
        interfaceLogMapper.insert(interfaceLog);

        JSONArray array = JSONUtil.parseArray(JSONUtil.toJsonStr(map.get("data")));
        List<CustomerErpDTO> customerErpDTOS = JSONUtil.toList(array, CustomerErpDTO.class);
        if(IterUtil.isNotEmpty(data)&&"10000".equals(codestr)){
            customerErpDTOS.stream().forEach(i->{
                BaseCustomerSupplier baseCustomerSupplier = new BaseCustomerSupplier(i);
                baseCustomerSupplierMapper.insert(baseCustomerSupplier);
            });
        }

        //同步成功之后进行回调
        HashMap<String, Object> paramMapRes = new HashMap<>();
        List<String> deptids = customerErpDTOS.stream().map(i -> i.getCustomerCode()).collect(Collectors.toList());
        paramMapRes.put("codes", deptids);
        paramMapRes.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String resp= HttpUtil.post(IpAddressConstant.ERP_IP+"/basic/customer/syncStateUpdate", JSONUtil.toJsonStr(paramMapRes));
        Map mapResp = JSONUtil.toBean(resp, Map.class);
        String code = (String)mapResp.get("code");

        //添加日志
        InterfaceLog interfaceLog2 = new InterfaceLog();
        interfaceLog2.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog2.setStartTime(new Date());
        interfaceLog2.setInterfaceDirection("MOM->ERP");
        interfaceLog2.setPacketSize("");
        interfaceLog2.setInterfaceRequest(JSONUtil.toJsonStr(paramMapRes));
        interfaceLog2.setEndTime(new Date());
        interfaceLog2.setInterfaceResponse(JSONUtil.toJsonStr(resp));
        long be2 = DateUtil.between(interfaceLog2.getStartTime(), interfaceLog2.getEndTime(), DateUnit.MS);
        interfaceLog2.setTimeConsuming(String.valueOf(be2));
        interfaceLog2.setCreateTime(new Date());
        interfaceLog2.setInterfaceType("http");
        interfaceLog2.setInterfaceName("客户数据同步回调");
        interfaceLogMapper.insert(interfaceLog2);

        if(!"10000".equals(code)){
            throw new CustomException("客户数据同步回调失败");
        }

        return "同步成功";
    }

    @Override
    public String getSupplier() {

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String res= HttpUtil.get(IpAddressConstant.ERP_IP+"/basic/vendor/syncVendorList", paramMap);
        Map map = JSONUtil.toBean(res, Map.class);
        List data = (List)map.get("data");
        String codestr = (String)map.get("code");

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("MOM->ERP");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(paramMap));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(JSONUtil.toJsonStr(res));
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("供应商数据同步");
        interfaceLogMapper.insert(interfaceLog);


        JSONArray array = JSONUtil.parseArray(JSONUtil.toJsonStr(map.get("data")));
        List<CustomerErpDTO> customerErpDTOS = JSONUtil.toList(array, CustomerErpDTO.class);
        if(IterUtil.isNotEmpty(data)&&"10000".equals(codestr)){
            customerErpDTOS.stream().forEach(i->{
                BaseCustomerSupplier baseCustomerSupplier = new BaseCustomerSupplier();
                baseCustomerSupplier.setCode(i.getVendorCode());
                baseCustomerSupplier.setName(i.getVendorName());
                baseCustomerSupplier.setBaseDescribe(i.getCustomerAbbName());
                baseCustomerSupplier.setClassCode(i.getVcCode());
                baseCustomerSupplier.setAddress(i.getAddress());
                baseCustomerSupplier.setUpdateTime(i.getUpdateDate());
                baseCustomerSupplierMapper.insert(baseCustomerSupplier);
            });
        }
        //同步成功之后进行回调
        HashMap<String, Object> paramMapRes = new HashMap<>();
        List<String> deptids = customerErpDTOS.stream().map(i -> i.getCustomerCode()).collect(Collectors.toList());
        paramMapRes.put("codes", deptids);
        paramMapRes.put("checkFlag", IpAddressConstant.ERP_CHECK_FLAG);
        String resp= HttpUtil.post(IpAddressConstant.ERP_IP+"/basic/vendor/syncStateUpdate", JSONUtil.toJsonStr(paramMapRes));
        Map mapResp = JSONUtil.toBean(resp, Map.class);
        String code = (String)mapResp.get("code");

        //添加日志
        InterfaceLog interfaceLog2 = new InterfaceLog();
        interfaceLog2.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog2.setStartTime(new Date());
        interfaceLog2.setInterfaceDirection("MOM->ERP");
        interfaceLog2.setPacketSize("");
        interfaceLog2.setInterfaceRequest(JSONUtil.toJsonStr(paramMapRes));
        interfaceLog2.setEndTime(new Date());
        interfaceLog2.setInterfaceResponse(JSONUtil.toJsonStr(resp));
        long be2 = DateUtil.between(interfaceLog2.getStartTime(), interfaceLog2.getEndTime(), DateUnit.MS);
        interfaceLog2.setTimeConsuming(String.valueOf(be2));
        interfaceLog2.setCreateTime(new Date());
        interfaceLog2.setInterfaceType("http");
        interfaceLog2.setInterfaceName("供应商数据同步回调");
        interfaceLogMapper.insert(interfaceLog2);

        if(!"10000".equals(code)){
            throw new CustomException("客户数据同步回调失败");
        }

        return "同步成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult syncUsers(List<UsersErpDTO> userList) {
        if (CollectionUtils.isEmpty(userList)){
            return AjaxResult.error("用户数据为空");
        }

        int count = 0;
        //进行用户新增
        for (int i = 0; i < userList.size(); i++){
            UsersErpDTO usersErpDTO = userList.get(i);
            SysUser sysUser = new SysUser();
            sysUser.setUserName(usersErpDTO.getJobNum());
            sysUser.setNickName(usersErpDTO.getPersonName());
            if (null != usersErpDTO.getDeptNum() && !isNumeric(usersErpDTO.getDeptNum())){
                throw new RuntimeException(String.format("用户：%s,部门编码非数字", usersErpDTO.getJobNum()));
            }
            sysUser.setDeptId(Long.valueOf(usersErpDTO.getDeptNum()));
            sysUser.setSex(usersErpDTO.getSex());
            if("true".equals(usersErpDTO.getDelFlag())){
                sysUser.setDelFlag("2");//2代表删除
            }
            if(StrUtil.isNotEmpty(usersErpDTO.getUpdateDate())){
                sysUser.setUpdateTime(DateUtil.parse(usersErpDTO.getUpdateDate().replace("/","-"),"yyyy-MM-dd"));
            }
//            SysUser use = sysUserMapper.selectUserById(sysUser.getUserId());
            SysUser use = sysUserMapper.selectUserByUserName(sysUser.getUserName());
            if(ObjectUtil.isNotNull(use)){
                sysUserMapper.updateUser(sysUser);
            }else {
                sysUserMapper.insertUser(sysUser);
            }
            count++;
        }

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("ERP->MOM");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(userList));
        interfaceLog.setEndTime(new Date());
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("同步用户信息");
        interfaceLog.setInterfaceResponse(String.format("同步成功：%s 条数据", userList.size()));
        interfaceLogMapper.insert(interfaceLog);

        //将用户数据同步到mes
//        this.sedUserMes(userList);

        return AjaxResult.success("同步成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult syncDepts(List<DeptErpDTO> deptList) {
        if (CollectionUtils.isEmpty(deptList)){
            return AjaxResult.error("部门数据为空");
        }

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("MOM->ERP");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(deptList));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(JSONUtil.toJsonStr(AjaxResult.success()));
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLogMapper.insert(interfaceLog);

        //进行部门新增
        deptList.stream().forEach(i->{
            SysDept sysDept = new SysDept();
            String deptCode = i.getDeptCode();
            if(deptCode.length()==2){
                sysDept.setParentId(0L);
            }else if(StrUtil.isNotEmpty(deptCode)){
                sysDept.setParentId(Long.valueOf(deptCode.substring(0,deptCode.length()-2)));
            }
            sysDept.setDeptId(Long.valueOf(deptCode));
            sysDept.setDeptName(i.getDeptName());
            sysDept.setPhone(i.getDeptTel());
            if("true".equals(i.getDelFlag())){
                sysDept.setDelFlag("2");//2代表删除
            }
            sysDept.setOrderNum(i.getDeptGrade());
            SysDept de = sysDeptMapper.selectDeptById(sysDept.getDeptId());
            if(ObjectUtil.isNotNull(de)){
                sysDeptMapper.updateDept(sysDept);
            }else {
                sysDeptMapper.insertDept(sysDept);
            }
        });
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult syncSupplier(List<CustomerErpDTO> supplierList) {
        if (CollectionUtils.isEmpty(supplierList)){
            return AjaxResult.error("供应商数据为空");
        }

        //添加日志
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("MOM->ERP");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(supplierList));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse(JSONUtil.toJsonStr(AjaxResult.success()));
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("供应商数据同步");
        interfaceLogMapper.insert(interfaceLog);

        supplierList.stream().forEach(i->{
            BaseCustomerSupplier baseCustomerSupplier = new BaseCustomerSupplier();
            baseCustomerSupplier.setCode(i.getVendorCode());
            baseCustomerSupplier.setName(i.getVendorName());
            baseCustomerSupplier.setBaseDescribe(i.getCustomerAbbName());
            baseCustomerSupplier.setClassCode(i.getVcCode());
            baseCustomerSupplier.setSupplierId(i.getId());
            baseCustomerSupplier.setAddress(i.getAddress());
            baseCustomerSupplier.setCreateTime(i.getCreateDate());
            baseCustomerSupplier.setUpdateTime(i.getUpdateDate());
            baseCustomerSupplier.setZlk(i.getZlk());
            baseCustomerSupplierMapper.insert(baseCustomerSupplier);
        });
        return AjaxResult.success();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");
        return pattern.matcher(str).matches();
    }
}
